/*
 * Comboes.java
 *
 * Created on July 19, 2005, 8:04 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package ge.ktapestry.propertyselections;

import org.apache.tapestry.form.IPropertySelectionModel;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.List;
import java.util.TreeMap;
import org.apache.log4j.Logger;

public class Comboes extends SqlMapClientDaoSupport 
{

    /**
     * Creates a new instance of Comboes
     */
    private volatile TreeMap<String, IPropertySelectionModel> comboMap;
    private ComboList labels;
    private String[] comboListNames;
    private boolean initialized = false;
    private long rescanIntervalMinutes = 60;
    Logger logger = Logger.getLogger(Comboes.class);

    public Comboes()
    {

    }

    public synchronized void init()
    {
        logger.info("Initializaing comboes");

        TreeMap<String, IPropertySelectionModel> map = null;
        try
        {
            map = new TreeMap<String, IPropertySelectionModel>();
            for (int i = 0; i < getComboListNames().length; i++)
            {
                List list = getSqlMapClientTemplate().queryForList("comboesMap.combo" + getComboListNames()[i], null);
                ComboList comboList = new ComboList();
                comboList.setOptions(list);
                map.put(getComboListNames()[i], comboList);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        setComboMap(map);
//        if (!initialized)
//        {
//            initialized = true;
//            Thread t = new Thread(this);
//            t.setDaemon(true);
//            logger.info("Starting  daemon thread");
//            t.start();
//        }
    }

    public TreeMap<String, IPropertySelectionModel> getComboMap()
    {
        return comboMap;
    }

    public void setComboMap(TreeMap<String, IPropertySelectionModel> comboMap)
    {
        this.comboMap = comboMap;
    }

    public IPropertySelectionModel getCombo(String str)
    {
        return comboMap.get(str);
    }

    public ComboList getLabels()
    {
        return labels;
    }

    public void setLabels(ComboList labels)
    {
        this.labels = labels;
    }

    public String[] getComboListNames()
    {
        return comboListNames;
    }

    public void setComboListNames(String[] comboListNames)
    {
        this.comboListNames = comboListNames;
    }

//    public void run()
//    {
//        if (initCount.incrementAndGet() > 1)
//        {
//            throw new RuntimeException("woooooooooooooq");
//        }
//        try
//        {
//            while (true)
//            {
//                logger.info("Starting sleep");
//                Thread.sleep(rescanIntervalMinutes * 60000);
//                this.init();
//            }
//        }
//        catch (Exception ex)
//        {
//            ex.printStackTrace();
//        }
//
//    }

    /**
     * @return the rescanInterval
     */
    public long getRescanIntervalMinutes()
    {
        return rescanIntervalMinutes;
    }

    /**
     * @param rescanInterval the rescanInterval to set
     */
    public void setRescanIntervalMinutes(long rescanInterval)
    {
        this.rescanIntervalMinutes = rescanInterval;
    }
}
